/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.common.core.auth.client.helper;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.medusa.gruul.common.core.auth.client.context.*;
import com.medusa.gruul.common.core.auth.model.CompleteUser;
import com.medusa.gruul.common.core.auth.model.User;
import com.medusa.gruul.common.core.auth.model.UserExtends;
import com.medusa.gruul.common.core.auth.model.constant.Keys;
import com.medusa.gruul.common.core.auth.model.enums.AuthPlatform;
import com.medusa.gruul.common.core.auth.model.enums.Platform;
import com.medusa.gruul.common.core.helper.RedisKeyHelper;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import com.medusa.gruul.common.redis.RedisManager;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author 张治保
 * date 2021/12/1
 */
public class ISecurity {

    private ISecurity(){}

    /**
     * 获取客户端ip地址
     */
    public static S<String> ip(){
        return S.of(
            IPContextHolder.get()
        );
    }
    /**
     * 获取当前请求版本号
     */
    public static S<String> version(){
        return S.of(
            VersionContextHolder.get()
        );
    }
    /**
     * 获取tanentId
     */
    public static S<String> tenantId(){
        return S.of(
            TenantContextHolder.getTenantId()
        );
    }
    /**
     * 获取 shopId
     */
    public static S<String> shopId(){
        return S.of(
            ShopContextHolder.getShopId()
        );
    }
    /**
     * 获取当前请求客户端类型
     */
    public static S<Platform> platform(){
        return S.of(
         PlatformContextHolder.get()
        );
    }
    /**
     * 获取当前请求客户端认证类型
     */
    public static S<AuthPlatform> authPlatform(){
        return S.of(
            AuthPlatformContextHolder.get()
        );
    }
    /**
     * 获取当前请求token
     */
    public static S<String> token(){
        return S.of(
            TokenContextHolder.get()
        );
    }

    /**
     * 获取当前请求用户信息
     */
    public static S<User> user(){
        CompleteUser completeUser = UserContextHolder.get();
        return S.of(completeUser == null?null: completeUser.getUser());
    }
    /**
     * 获取字符串形式user
     */
    public  static S<String> userStr(){
        CompleteUser completeUser = UserContextHolder.get();
        return S.of(completeUser == null?null: completeUser.getUserStr());
    }
    /**
     * 获取jti
     */
    public static S<String> jti(){
        CompleteUser completeUser = UserContextHolder.get();
        return S.of(completeUser == null?null: completeUser.getJti());
    }
    /**
     * 获取用户补充信息
     */
    public static S<UserExtends> userExtends(){
        return S.of(
            UserExtendsContextHolder.get()
        );
    }
    /**
     * 使当前平台当前用户token失效
     */
    public static void invalid(){
        ISecurity.invalid(ISecurity.platform().must(),ISecurity.user().must().getUserId());
    }
    /**
     * 使指定平台的token失效
     */
    public static void  invalid(Platform platform,String userId){
        if (platform ==null || StrUtil.isBlank(userId)){
            return;
        }
        RedisManager.getInstance().del(RedisKeyHelper.key(Keys.USER_PLATFORM_JTI,platform.name(),userId));
    }
    /**
     * 当前用户额外失效
     */
    public static void invalidExtends(){
        ISecurity.invalidExtends(ISecurity.user().must().getUserId());
    }

    /**
     * 使指定用户的额外失效
     * @param userId 的userId
     */
    public static void invalidExtends(String userId){
        if (StrUtil.isBlank(userId)){
            return;
        }
        RedisManager.getInstance().del(RedisKeyHelper.key(Keys.USER_EXTENDS ,userId));
    }

    /**
     * 更新最后修改时间缓存信息
     */
    public static void updateModifyTimeCache(String userId){
        RedisManager redis = RedisManager.getInstance();
        redis.set(
            RedisKeyHelper.key(Keys.USER_LAST_MODIFY_TIME, userId),
            LocalDateTime.now().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN))
        );
    }
}
